package com.nice.nice201110.service;

import com.nice.nice201110.entity.TDeliveryDel;
import com.nice.nice201110.entity.TLog;
import com.nice.nice201110.entity.TOtherRunningWater;
import com.nice.nice201110.mapper.TDeliveryDelMapper;
import com.nice.nice201110.mapper.TLogMapper;
import com.nice.nice201110.mapper.TOtherRunningWaterMapper;
import com.nice.nice201110.utils.NiceEnum;
import com.nice.nice201110.utils.NiceTableEnum;
import com.nice.nice201110.vo.BaseVo;
import com.nice.nice201110.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class TotherRunningWaterService {

    //获取其他流水账单dao对象
    @Autowired
    TOtherRunningWaterMapper tOtherRunningWaterMapper;

    //获取出账明细dao对象
    @Autowired
    TDeliveryDelMapper tDeliveryDelMapper;

    @Autowired
    TLogMapper tLogMapper;

    @Autowired
    private PlatformTransactionManager ransactionManager;

    private TLog tLog = new TLog();

    /** 查询数据 */
    public List<TOtherRunningWater> getList(BaseVo baseVo){
        return tOtherRunningWaterMapper.selectByPrimaryKeys(baseVo);
    }
    /** 单一查询  */
    public TOtherRunningWater getOne(UserVo userVo, Integer id){
        TOtherRunningWater tOtherRunningWater = tOtherRunningWaterMapper.selectByPrimaryKey(id);
        if(tOtherRunningWater!=null){
            tLog.setDate(new Date());
            tLog.setType(3);  //操作类型（新增0，修改1，删除2,查询3）
            tLog.setDocid(tOtherRunningWater.getId());
            tLog.setUsername(userVo.getName());
            tLog.setUserid(Integer.valueOf(userVo.getId()));
            tLog.setTtable(NiceTableEnum.form_other_running_water);
            tLog.setRemark("查看流水账单信息:"+tOtherRunningWater.toString());
            tLogMapper.insertSelective(tLog);
        }
        return tOtherRunningWater;
    }
    /** 插入信息 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean inserttotherRunningWater(UserVo userVo,TOtherRunningWater tOtherRunningWater){
        try {
            int num = tOtherRunningWaterMapper.insertSelective(tOtherRunningWater);
            if(num>0){
                tLog.setDate(new Date());
                tLog.setType(0);  //操作类型（新增0，修改1，删除2）
                tLog.setDocid(tOtherRunningWater.getId());
                tLog.setUsername(userVo.getName());
                tLog.setUserid(Integer.valueOf(userVo.getId()));
                tLog.setTtable(NiceTableEnum.form_other_running_water);
                tLog.setRemark("新增流水账单信息:"+tOtherRunningWater.toString());
                tLogMapper.insertSelective(tLog);
            }else{
                return false;
            }
            //根据流水账单信息生成对应的出库信息
            String Informationids = tOtherRunningWater.getInformationids();
            BigDecimal Price = tOtherRunningWater.getPrice();   //获取流水花费金额
            BigDecimal a = new BigDecimal (1);   //最少入账1块钱
            if((Informationids != null) && (Informationids.trim().length() > 0) && (Price.compareTo(a) > -1)) {
                Integer docid = tOtherRunningWater.getId();  //获取流水的id

                String[] InformationidsArr = Informationids.split(",");
                int InformationNum = InformationidsArr.length;  //获取流水账单对应基础信息表总数
                BigDecimal pPrice = Price.divide(BigDecimal.valueOf(InformationNum));  //获取每个基础信息的金钱
                BigDecimal tDeliveryDelNum = new BigDecimal (1);   // 默认数量0
                for(String Informationid:InformationidsArr){
                    TDeliveryDel tDeliveryDel = new TDeliveryDel();
                    tDeliveryDel.setType(NiceEnum.del_liushui);
                    tDeliveryDel.setAmountSpent(pPrice);
                    tDeliveryDel.setDeliveryTime(new Date());
                    tDeliveryDel.setInformationid(Informationid);//基本信息id
                    tDeliveryDel.setParentid(docid);
                    tDeliveryDel.setName(docid);
                    tDeliveryDel.setNumber(tDeliveryDelNum);
                    int tDeliveryDelInsertNum = tDeliveryDelMapper.insertSelective(tDeliveryDel);
                    if(tDeliveryDelInsertNum<=0){
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return false;
                    }
                }
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
    /** 删除信息 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean deleteTOtherRunningWater(UserVo userVo, Integer id){
        try {
            TOtherRunningWater tOtherRunningWater = tOtherRunningWaterMapper.selectByPrimaryKey(id);
            if(tOtherRunningWater==null) {
                return false;
            }
            String tOtherRunningWaterStr = tOtherRunningWater.toString();
            int num = tOtherRunningWaterMapper.deleteByPrimaryKey(id);
            if(num>0){
                tLog.setDate(new Date());
                tLog.setType(2);  //操作类型（新增0，修改1，删除2）
                tLog.setDocid(id);
                tLog.setUsername(userVo.getName());
                tLog.setUserid(Integer.valueOf(userVo.getId()));
                tLog.setTtable(NiceTableEnum.form_other_running_water);
                tLog.setRemark("删除流水账目信息:" + tOtherRunningWaterStr);
                tLogMapper.insertSelective(tLog);
                int tDeliveryDelInsertNum = tDeliveryDelMapper.deleteByParentId(id, NiceEnum.del_liushui);
                if(tDeliveryDelInsertNum<=0){
                    tLog.setDate(new Date());
                    tLog.setType(2);  //操作类型（新增0，修改1，删除2）
                    tLog.setDocid(id);
                    tLog.setUsername(userVo.getName());
                    tLog.setUserid(Integer.valueOf(userVo.getId()));
                    tLog.setTtable(NiceTableEnum.form_other_running_water);
                    tLog.setRemark("删除流水账目明细信息异常，此流水账目无明细，请自行核实!");
                    tLogMapper.insertSelective(tLog);
                }
            }else{
                return false;
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
}
